var t = void 0,
  a = void 0,
  e = void wx.getWindowInfo().pixelRatio,
  c = void 0,
  i = void 0,
  o = void 0,
  s = void 0,
  u = void 0,
  h = void 0,
  r = void 0,
  d = void 0,
  n = 750 / wx.getWindowInfo().windowWidth,
  p = 200,
  g = void 0;

Page({
  data: {
    // imageSrc: "../../images/bg.png",
    isShowImg: false,
    cropperInitW: 750,
    cropperInitH: 750,
    cropperW: 750,
    cropperH: 750,
    cropperL: 0,
    cropperT: 0,
    scaleP: 0,
    cutL: 0,
    cutT: 0,
    cutB: 750,
    cutR: "100%",
    qualityWidth: g,
    innerAspectRadio: n
  },
  onLoad: function (t) {},
  onReady: function () {
    // this.loadImage();
  },
  async getImage() {
    wx.showActionSheet({
      itemList: ['从聊天会话选择', '从相册选择', '拍照'],
      success: async res => {
        if (res.tapIndex === 0) {
          let res2 = await wx.chooseMessageFile({
            count: 1,
            type: 'image',
          })
          this.setData({
            imageSrc: res2.tempFiles[0].path
          })
        } else if (res.tapIndex === 1) {
          // 选择本地图片
          let res2 = await wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album'],
            sizeType: ['original'],
          })
          this.setData({
            imageSrc: res2.tempFiles[0].tempFilePath
          })
        } else {
          // 选择本地图片
          let res2 = await wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['camera'],
            sizeType: ['original'],
          })
          this.setData({
            imageSrc: res2.tempFiles[0].tempFilePath
          })
        }
        console.log('图片信息', this.imgSrc)
        this.loadImage();
      },
      fail: err => {
        console.log(err.errMsg)
      }
    })
  },
  // 
  loadImage: function () {
    wx.showLoading({
      title: "图片加载中..."
    })
    wx.getImageInfo({
      src: this.data.imageSrc,
      success: res => {
        g = r = res.width, d = res.height;
        var e = r > d ? r : d;
        p = e > p ? p : e, (h = r / d) >= 1 ? this.setData({
          cropperW: 750,
          cropperH: 750 / h,
          cropperL: Math.ceil(0),
          cropperT: Math.ceil((750 - 750 / h) / 2),
          cutL: Math.ceil((0 + p) / 2),
          cutT: Math.ceil((750 / h - (750 / h - p)) / 2),
          cutR: Math.ceil((0 + p) / 2),
          cutB: Math.ceil((750 / h - (750 / h - p)) / 2),
          scaleP: r / 750,
          qualityWidth: g,
          innerAspectRadio: h
        }) : this.setData({
          cropperW: 750 * h,
          cropperH: 750,
          cropperL: Math.ceil((750 - 750 * h) / 2),
          cropperT: Math.ceil(0),
          cutL: Math.ceil((750 * h - 750 * h) / 2),
          cutT: Math.ceil((750 - p) / 2),
          cutB: Math.ceil((750 - p) / 2),
          cutR: Math.ceil((750 * h - 750 * h) / 2),
          scaleP: r / 750,
          qualityWidth: g,
          innerAspectRadio: h
        })
        this.setData({
          isShowImg: true
        })
        wx.hideLoading();
      },
      fail: err => {
        wx.hideLoading()
        console.error(err)
      }
    });
  },
  contentStartMove: function (e) {
    t = e.touches[0].pageX, a = e.touches[0].pageY;
  },
  contentMoveing: function (e) {
    var c = (t - e.touches[0].pageX) * n,
      i = (a - e.touches[0].pageY) * n;
    c > 0 ? this.data.cutL - c < 0 && (c = this.data.cutL) : this.data.cutR + c < 0 && (c = -this.data.cutR),
      i > 0 ? this.data.cutT - i < 0 && (i = this.data.cutT) : this.data.cutB + i < 0 && (i = -this.data.cutB),
      this.setData({
        cutL: this.data.cutL - c,
        cutT: this.data.cutT - i,
        cutR: this.data.cutR + c,
        cutB: this.data.cutB + i
      }), t = e.touches[0].pageX, a = e.touches[0].pageY;
  },
  getImageInfo: function () {
    if (!this.data.imageSrc) {
      return wx.showToast({
        title: '请先选择图片',
        icon: 'error'
      })
    }
    var t = this;
    wx.showLoading({
      title: "图片生成中..."
    });
    var a = wx.createCanvasContext("myCanvas");
    a.drawImage(t.data.imageSrc, 0, 0, r, d), a.draw(!0, function () {
      var a = (t.data.cropperW - t.data.cutL - t.data.cutR) / t.data.cropperW * r,
        e = (t.data.cropperH - t.data.cutT - t.data.cutB) / t.data.cropperH * d,
        c = t.data.cutL / t.data.cropperW * r,
        i = t.data.cutT / t.data.cropperH * d;
      wx.canvasToTempFilePath({
        x: c,
        y: i,
        width: a,
        height: e,
        destWidth: a,
        destHeight: e,
        quality: .5,
        canvasId: "myCanvas",
        success: t => {
          wx.hideLoading()

          wx.previewImage({
            current: "",
            urls: [t.tempFilePath]
          });
        }
      });
    });
  },
  dragStart: function (t) {
    e = t.touches[0].pageX, c = t.touches[0].pageY, i = this.data.cutL, s = this.data.cutR,
      u = this.data.cutB, o = this.data.cutT;
  },
  dragMove: function (t) {
    switch (t.target.dataset.drag) {
      case "right":
        a = (e - t.touches[0].pageX) * n, s + a < 0 && (a = -s), this.setData({
          cutR: s + a
        });
        break;

      case "left":
        a = (e - t.touches[0].pageX) * n, i - a < 0 && (a = i), i - a > this.data.cropperW - this.data.cutR && (a = i - (this.data.cropperW - this.data.cutR)),
          this.setData({
            cutL: i - a
          });
        break;

      case "top":
        a = (c - t.touches[0].pageY) * n, o - a < 0 && (a = o), o - a > this.data.cropperH - this.data.cutB && (a = o - (this.data.cropperH - this.data.cutB)),
          this.setData({
            cutT: o - a
          });
        break;

      case "bottom":
        var a = (c - t.touches[0].pageY) * n;
        u + a < 0 && (a = -u), this.setData({
          cutB: u + a
        });
        break;

      case "rightBottom":
        var h = (e - t.touches[0].pageX) * n,
          r = (c - t.touches[0].pageY) * n;
        u + r < 0 && (r = -u), s + h < 0 && (h = -s), this.setData({
          cutB: u + r,
          cutR: s + h
        });
    }
  },
  onShareAppMessage() {
    return {
      title: "图片裁剪神器，只留你想要的地方",
      path: "/pages/imageTools/pages/imageCut/index",
      imageUrl: '/images/function_icon/imageCut.jpeg',
    }
  },
  onShareTimeline() {
    return {
      title: '图片裁剪神器，只留你想要的地方',
      imageUrl: '/images/function_icon/imageCut.jpeg',
    }
  },
});